package com.usmarter.server.services;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.codehaus.jettison.json.JSONObject;

public class HTTPUtil {

	public static HttpResponse get(String url) {
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet getMethod = new HttpGet(url);
		HttpResponse response = null;

		try {
			response = httpClient.execute(getMethod);
		} catch (Exception e) {
			System.out.println("Failed to get url: " + url + '\n' + e);
		}
		return response;
	}

	public static HttpResponse post(String url,String json) {
	    // Create a new HttpClient and Post Header
		HttpResponse response = null;
	    HttpClient httpclient = new DefaultHttpClient();
	    HttpParams myParams = new BasicHttpParams();
	    HttpConnectionParams.setConnectionTimeout(myParams, 10000);
	    HttpConnectionParams.setSoTimeout(myParams, 10000);
	    try {
	        HttpPost httpPost = new HttpPost(url);
	        StringEntity se = new StringEntity(json); 
	        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
	        httpPost.setEntity(se); 
	        response = httpclient.execute(httpPost);
//	        String temp = EntityUtils.toString(response.getEntity());
	    } catch (Exception e) {

	    } 
	    return response;
	}
//	public static HttpResponse post(String url, String data) {
//		StringEntity requestEntity = new StringEntity(
//			    data,
//			    "application/json",
//			    "UTF-8");
//
//			PostMethod postMethod = new PostMethod("http://example.com/action");
//			postMethod.setRequestEntity(requestEntity);
//
//			int statusCode = httpClient.executeMethod(postMethod);
//	}

}
